package com.example.demo.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-06-18
 * Time: 22:01
 */
public class Test2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class clazz = Class.forName("com.example.demo.reflect.Student");
//        //1.返回所有公共构造方法对象的数组
//        Constructor[] cons1 = clazz.getConstructors();
//        for (Constructor con : cons1) {
//            System.out.println(con);
//        }
//        System.out.println("================================");
//        //2.返回所有构造方法对象的数组包括私有+protected
//        Constructor[] cons2 = clazz.getDeclaredConstructors();
//        for (Constructor con : cons2) {
//            System.out.println(con);
//        }
//        System.out.println("================================");
//        //3.返回单个公共构造方法对象
//        Constructor const1 = clazz.getConstructor();
//        System.out.println(const1);
//        System.out.println("================================");
//        Constructor const2 = clazz.getConstructor(String.class);
//        System.out.println(const2);
//        System.out.println("================================");
//        Constructor const3 = clazz.getConstructor(String.class,int.class);
//        System.out.println(const3);
//        //4.返回单个的所有构造方法对象
        Constructor const4 = clazz.getDeclaredConstructor(String.class,int.class,float.class);
        System.out.println(const4);
        const4.setAccessible(true);
        //创建对象
        Object o = const4.newInstance("张三", 18,1.1f);
    }
}